package com.fw.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fw.model.Ecommerce;
import com.fw.service.EcommerceService;
import com.fw.service.impl.EcommerceServiceImpl;
import com.jfinal.core.Controller;

public class ECommerceController extends Controller {
	EcommerceService ecommerceService = null;
	public void toEcommerceListView() {
		renderJsp("/WEB-INF/jsp/e_commerce_list.jsp");
	}

	public void query_allEcommerceByPage() throws UnsupportedEncodingException {
		ecommerceService = new EcommerceServiceImpl();
		StringBuffer sql = new StringBuffer();
		HttpServletRequest request = getRequest();
		request.setCharacterEncoding("utf-8");
		HttpServletResponse response = getResponse();
		response.setContentType("text/html;charset = utf-8");
		// 显示的起始索引
		int limitStart = Integer.parseInt(getPara("start").toString());
		// 显示的行数
		int limitLength = Integer.parseInt(getPara("length").toString());
		String draw = "0";
		draw = getPara("draw");
		List<Object> par = new ArrayList<Object>();
		int totalData = ecommerceService.query_totalEcommerceByPageService(sql.toString(),par);
		List<Ecommerce> rs = ecommerceService.query_allEcommerceByPageService(sql.toString(),
				limitStart, limitLength,par);
		Map<Object, Object> info = new HashMap<Object, Object>();
		info.put("data", rs);
		info.put("recordsTotal", totalData);
		info.put("recordsFiltered", totalData);
		info.put("draw", draw);
		renderJson(info);
	}
	public void toAddEcommerceView(){
		renderJsp("/WEB-INF/jsp/add_e_commerce.jsp");
	}
	public void addEcommerce(){
		Ecommerce ecommerce = getModel(Ecommerce.class);
		ecommerceService = new EcommerceServiceImpl();
		String message = null;
		boolean flag = ecommerceService.add_ecommerceService(ecommerce);
		if(flag){
			message = "添加成功";
			setAttr("message",message);
		}
		else{
			message = "添加失败";
			setAttr("message",message);
		}
		renderJsp("/WEB-INF/jsp/cue_message.jsp");
	}
	public void toUpdateEcommerce(){
		Ecommerce ecommerce = getModel(Ecommerce.class);
		ecommerceService = new EcommerceServiceImpl();
		Ecommerce e=  ecommerceService.query_ecommerceByIdService(ecommerce);
		setAttr("ecommerce", e);
		renderJsp("/WEB-INF/jsp/update_e_commerce.jsp");
	}
	public void updateEcommerce(){
		Ecommerce ecommerce = getModel(Ecommerce.class);
		ecommerceService = new EcommerceServiceImpl();
		boolean flag = ecommerceService.update_ecommerceByIdService(ecommerce);
		String message = null;if(flag){
			message = "编辑成功";
			setAttr("message",message);
		}
		else{
			message = "编辑失败";
			setAttr("message",message);
		}
		renderJsp("/WEB-INF/jsp/cue_message.jsp");
	}
	public void delete_ecommerce(){
		Ecommerce ecommerce = getModel(Ecommerce.class);
		ecommerceService = new EcommerceServiceImpl();
		boolean flag = ecommerceService.delete_ecommerceService(ecommerce);
		renderJson(flag);
	}
}
